﻿
Partial Class UCMain_Edit_Engineer_password
    Inherits System.Web.UI.Page


    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If IsNothing(Session("section")) Then
            Session.Clear()
            Response.Redirect("../Login.aspx")
        End If

        If Not IsPostBack Then
            'img.Visible = False
            'lblerror.Text = ""
            clearscreen()
            If Not IsNothing(Session("ID_USER")) Then
                ' edit
                loadData()
            Else
                ' add

            End If

        End If
    End Sub
    Sub clearscreen()
        txtName.Text = ""
        TxtSection.Text = ""
        TxtSurname.Text = ""
        TxtTel.Text = ""
        TxtOld_pass.Text = ""
        TxtNew_pass.Text = ""
        TxtRenew_pass.Text = ""
    End Sub
    Sub loadData()
        Dim sql As String = ""
        ' sql = " select * from user_table where section='MMC'"
        sql = " select * from user_table where 1=1"
        sql += " and [ID_USER] = " & Session("ID_USER") & ""
        ' End If
        ' sql += " order by ID_PICOP "
        Dim dt As New Data.DataTable
        dt = clsCommand.SQL_exec(sql)
        If dt.Rows.Count > 0 Then
            TxtName.Text = dt.Rows(0)("firstname").ToString
            TxtSection.Text = dt.Rows(0)("Section").ToString
            TxtSurname.Text = dt.Rows(0)("lastname").ToString
            TxtTel.Text = dt.Rows(0)("tel").ToString
        End If
    End Sub

    Function checkBeforeSave() As Boolean
        Dim rs As Boolean = True
        Dim msg As String = " กรุณาระบุ "
        img.Visible = True
        lblerror.Visible = True

        If TxtName.Text = "" Then
            msg += " , Name"
            rs = False
        End If

        If TxtSurname.Text = "" Then
            msg += " , Surname"
            rs = False
        End If

        If TxtTel.Text = "" Then
            msg += " , Tel"
            rs = False
        End If

        If rs = False Then
            img.Visible = True
            lblerror.Visible = True
            lblerror.Text = Utility.clsAlertMsg.infoMessage(img, "W001", msg)
        End If

        Return rs
    End Function


    Protected Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        If checkBeforeSave() Then
            If Not IsNothing(Session("ID_USER")) Then
                Dim id_user As String = Session("ID_USER")
                'check pass
                Dim sqlchk As String = " select PASSWORD_PICOP from [user_table]  where ID_USER= " & id_user & ""
                Dim dt As New Data.DataTable
                Dim oldpass As String = ""
                dt = clsCommand.SQL_exec(sqlchk)
                If dt.Rows.Count > 0 Then
                    oldpass = dt.Rows(0)("PASSWORD_PICOP")
                End If
                ' edit
                Dim sqle As String = ""
                ' check oldpass 
                If TxtOld_Pass.Text <> oldpass Then
                    clearpass()
                    img.Visible = True
                    lblerror.Text = Utility.clsAlertMsg.infoMessage(img, "I003", "รหัสผ่านไม่ถูกต้อง กรุณาตรวจสอบ")
                    Exit Sub
                End If
                If TxtNew_Pass.Text <> TxtRenew_pass.Text Then
                    clearpass()
                    img.Visible = True
                    lblerror.Text = Utility.clsAlertMsg.infoMessage(img, "I003", "รหัสผ่านไม่ถูกต้อง กรุณาตรวจสอบ")
                    Exit Sub
                End If
                sqle = " update [user_table] set "
                sqle += " PASSWORD_PICOP='" & TxtNew_Pass.Text & "'"
                sqle += " ,FIRSTNAME='" & TxtName.Text & "'"
                sqle += " ,LASTNAME='" & TxtSurname.Text & "'"
                sqle += " ,TEL='" & TxtTel.Text & "'"
                sqle += " where ID_USER= " & id_user & ""

                Dim err As String = ""
                err = clsCommand.SQL_cmd(sqle)
                If err = "" Then
                    'img.Visible = True
                    'lblerror.Text = Utility.clsAlertMsg.infoMessage(img, "I001")
                    Utility.clsAlertMsg.alert_location(Page, "บันทึกข้อมูลเรียบร้อย", "Section_Profile.aspx")
                Else
                    img.Visible = True
                    lblerror.Text = Utility.clsAlertMsg.infoMessage(img, "E004", err)
                End If
            End If
        End If
    End Sub

    Sub clearpass()
        TxtNew_Pass.Text = ""
        TxtRenew_pass.Text = ""
        TxtOld_Pass.Text = ""
    End Sub

End Class


